import request from '@/utils/request'
import { getToken } from '@/utils/auth'

// 统一前缀
var prefix = '/fileService'

//条件查询文件
export function lookFile(file) {
  return request({
    url: `${prefix}/file`,
    method: 'GET',
    params: file,
  })
}

//上传文件返回ID
export function uploadFile(file) {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: `${process.env.VUE_APP_ORIGIN_API}${prefix}/file/upload`,
      filePath: file,
      name: 'file',
      header: {
        Authorization: getToken(),
      },
      success: (res) => {
        resolve(JSON.parse(res.data))
      },
      fail: (error) => {
        reject(error)
      },
    })
  })
}

//分片上传
export function uploadFileCheck(file) {
  return request({
    url: `${prefix}/file/uploadChunk`,
    method: 'POST',
    data: file,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })
}

//删除文件
export function deleteFile(ids) {
  return request({
    url: `${prefix}/file/delete`,
    method: 'DELETE',
    data: ids,
  })
}

export function download(id) {
  return request({
    url: `${prefix}/file/download`,
    method: 'GET',
    params: { id: id },
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    },
  })
}

//合并文件
export function mergeFile(formData) {
  return request({
    url: `${prefix}/file/merge`,
    timeout: 1 * 60 * 1000, //最大超时1小时
    method: 'POST',
    data: formData,
  })
}

//新增文件备注
export function addNote(file) {
  return request({
    url: `${prefix}/file/addNote`,
    method: 'POST',
    data: file,
  })
}

//根据ID查询文件信息
export function getOne(id) {
  return request({
    url: `${prefix}/file/getOne`,
    method: 'GET',
    params: { id: id },
  })
}
